<html>
<head><meta charset="utf-8"><title>Resolved type of a generic type · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html">Resolved type of a generic type</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="233328938"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233328938" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Flying-Toast <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233328938">(Apr 06 2021 at 14:34)</a>:</h4>
<p>If I have something like this:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="k">fn</span> <span class="nf">something_generic</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">inner</span>::<span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="n">something_generic</span>::<span class="o">&lt;</span><span class="kt">i32</span><span class="o">&gt;</span><span class="p">();</span><span class="w"></span>
</code></pre></div>
<p>and (within rustc) I have a reference to the <code>inner::&lt;T&gt;()</code> expression as a <code>rustc_hir::ExprKind::Call</code>,  is it possible to get a list of types that <code>T</code> has been <em>instantiated</em> to?</p>
<p>I'm trying to implement <a href="https://github.com/rust-lang/rust/pull/83908">this lint</a>, but I found that the lint breaks with something like that^^.</p>



<a name="233329584"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233329584" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Flying-Toast <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233329584">(Apr 06 2021 at 14:38)</a>:</h4>
<p>FWIW, the clippy lint that it's based off of also gives a false positive in this case (run clippy on <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=4218694806cf96336d20a802d6282281">this playground</a>).</p>



<a name="233330109"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233330109" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233330109">(Apr 06 2021 at 14:41)</a>:</h4>
<p>This isn't generally possible because downstream crates could call it (if public) with arbitrary types they define</p>



<a name="233330573"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233330573" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Flying-Toast <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233330573">(Apr 06 2021 at 14:45)</a>:</h4>
<p>Hm, so would it just be best for the lint to ignore cases with non-concrete types? If so, how do I detect if a <code>TyS</code> is concrete or not?</p>



<a name="233330866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233330866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233330866">(Apr 06 2021 at 14:47)</a>:</h4>
<p>Probably, yes. You can match on the type to detect this.</p>



<a name="233330943"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233330943" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233330943">(Apr 06 2021 at 14:47)</a>:</h4>
<p>the rustc docs are broken so I can't figure out the details</p>



<a name="233332417"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233332417" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> lcnr <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233332417">(Apr 06 2021 at 14:56)</a>:</h4>
<p>you can use <code>needs_subst</code> to detect if a type has generic parameters</p>



<a name="233336252"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233336252" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233336252">(Apr 06 2021 at 15:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Resolved.20type.20of.20a.20generic.20type/near/233330943">said</a>:</p>
<blockquote>
<p>the rustc docs are broken so I can't figure out the details</p>
</blockquote>
<p>you can use /beta for now</p>



<a name="233339457"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233339457" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233339457">(Apr 06 2021 at 15:22)</a>:</h4>
<p>okay, then you need to match <code>tys.kind</code> against <code>ty::Adt</code>, since that's the only case you care about <span class="user-mention" data-user-id="403257">@Flying-Toast</span></p>



<a name="233355461"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233355461" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Flying-Toast <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233355461">(Apr 06 2021 at 17:07)</a>:</h4>
<p>Thanks. So would <a href="https://github.com/rust-lang/rust/blob/526fbef8fa87950b7e449b1fde70328df29e31f4/compiler/rustc_lint/src/enum_intrinsics_non_enums.rs#L40">this</a> be correct to check if <code>t</code> not a generic type?</p>



<a name="233355706"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233355706" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233355706">(Apr 06 2021 at 17:08)</a>:</h4>
<p>why isn't checking for <code>is_enum</code> enough?</p>



<a name="233361234"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233361234" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Flying-Toast <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233361234">(Apr 06 2021 at 17:52)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink  [he/him]</span> <a href="#narrow/stream/182449-t-compiler.2Fhelp/topic/Resolved.20type.20of.20a.20generic.20type/near/233355706">said</a>:</p>
<blockquote>
<p>why isn't checking for <code>is_enum</code> enough?</p>
</blockquote>
<p>Because then <a href="https://play.rust-lang.org/?version=stable&amp;mode=debug&amp;edition=2018&amp;gist=aa03ece8f5b80a45267e766bd26d14d2">this</a> would trigger a false positive.</p>



<a name="233361374"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233361374" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233361374">(Apr 06 2021 at 17:53)</a>:</h4>
<p>Does <code>is_enum</code> really return true when called on <code>T</code>?</p>



<a name="233361545"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233361545" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Flying-Toast <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233361545">(Apr 06 2021 at 17:54)</a>:</h4>
<p><code>is_enum</code> returns false on a <code>T</code>, but the lint thinks that means that <code>T</code> is not an enum - however, it is possible that <code>T</code> is only ever instantiated with enum types.</p>



<a name="233362195"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/Resolved%20type%20of%20a%20generic%20type/near/233362195" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/Resolved.20type.20of.20a.20generic.20type.html#233362195">(Apr 06 2021 at 17:59)</a>:</h4>
<p>oh, right, sorry, you're checking for a different condition</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>